package com.jingdianjichi.subject.domain.service.Impl;

import com.alibaba.fastjson.JSON;
import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.enums.IsDeletedCodeEnum;
import com.jingdianjichi.subject.domain.Handler.subject.SubjectTypeHandler;
import com.jingdianjichi.subject.domain.Handler.subject.SubjectTypeHandlerFactory;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBo;
import com.jingdianjichi.subject.domain.service.SubjectInfoDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectInfo;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectInfoService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service

public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {

    @Resource
    private SubjectTypeHandlerFactory subjectTypeHandlerFactory;
    @Resource
    private SubjectInfoService subjectInfoService;
    @Resource
    private SubjectMappingService subjectMappingService;

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        if (log.isInfoEnabled()) {
            log.info("subjectCategoryService.queryPrimaryCategory.subjectInfoBo:{}", JSON.toJSONString(subjectInfoBo));
        }

        SubjectInfo subjectInfo = new SubjectInfo();
        BeanUtils.copyProperties(subjectInfoBo, subjectInfo);
        subjectInfo.setIsDeleted(IsDeletedCodeEnum.UN_DELETED.getCode());
        subjectInfoService.insert(subjectInfo);

        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfo.getSubjectType());
        subjectInfoBo.setId(subjectInfo.getId());
        handler.add(subjectInfoBo);

        List<Integer> categoryIds = subjectInfoBo.getCategoryIds();
        List<Integer> labelIds = subjectInfoBo.getLabelIds();


        List<SubjectMapping> subjectMappingList = new LinkedList<>();
        categoryIds.forEach(categoryId -> {
            labelIds.forEach(labelId -> {
                SubjectMapping subjectMapping = new SubjectMapping();
                subjectMapping.setSubjectId(subjectInfo.getId());
                subjectMapping.setCategoryId(Long.valueOf(categoryId));
                subjectMapping.setLabelId(Long.valueOf(labelId));

                subjectMapping.setIsDeleted(IsDeletedCodeEnum.UN_DELETED.getCode());
                subjectMappingList.add(subjectMapping);
            });
        });

        subjectMappingService.BatchInsert(subjectMappingList);
    }

    @Override
    public PageResult<SubjectInfoBo> getSubjectPage(SubjectInfoBo subjectInfoBo) {
        PageResult<SubjectInfoBo> pageResult = new PageResult<>();
        pageResult.setPageNo(subjectInfoBo.getPageNo());
        pageResult.setPageSize(subjectInfoBo.getPageSize());

        int start = (subjectInfoBo.getPageNo() - 1) * subjectInfoBo.getPageSize();

        SubjectInfo subjectInfo = new SubjectInfo();
        BeanUtils.copyProperties(subjectInfoBo, subjectInfo);

        int count = subjectInfoService.countByCondtion(subjectInfo, subjectInfoBo.getLabelId(), subjectInfoBo.getCategoryId());

        if (count == 0) {
            return pageResult;
        }

        List<SubjectInfo> subjectInfoList = subjectInfoService.getSubjectPage
                (subjectInfo, subjectInfoBo.getLabelId(), subjectInfoBo.getCategoryId(), start, subjectInfoBo.getPageSize());

        List<SubjectInfoBo> subjectInfoBoList = subjectInfoList.stream().map(subjectInfo1 -> {
            SubjectInfoBo subjectInfoBo1 = new SubjectInfoBo();
            BeanUtils.copyProperties(subjectInfo1, subjectInfoBo1);
            return subjectInfoBo1;
        }).collect(Collectors.toList());
        pageResult.setRecords(subjectInfoBoList);
        pageResult.setTotal(count);
        pageResult.setStart(start);
        return pageResult;
    }

//    @Override
//    public PageResult<SubjectInfoBo> querySubjectInfo(SubjectInfoBo subjectInfoBo) {
//
//
//
//        PageResult<SubjectInfoBo> pageResult = new PageResult<>();
//        pageResult.setPageNo(subjectInfoBo.getPageNo());
//        pageResult.setPageSize(subjectInfoBo.getPageSize());
//
//        int start = (subjectInfoBo.getPageNo() - 1) * subjectInfoBo.getPageSize();
//
//        SubjectInfo subjectInfo = new SubjectInfo();
//        BeanUtils.copyProperties(subjectInfoBo, subjectInfo);
//
//        int count = subjectInfoService.countByCondtion(subjectInfo, subjectInfoBo.getLabelId(), subjectInfoBo.getCategoryId());
//
//        if (count == 0) {
//            return pageResult;
//        }
//
//        List<SubjectInfo> subjectInfoList = subjectInfoService.getSubjectPage
//                (subjectInfo, subjectInfoBo.getLabelId(), subjectInfoBo.getCategoryId(), start, subjectInfoBo.getPageSize());
//
//        List<SubjectInfoBo> subjectInfoBoList = subjectInfoList.stream().map(subjectInfo1 -> {
//            SubjectInfoBo subjectInfoBo1 = new SubjectInfoBo();
//            BeanUtils.copyProperties(subjectInfo1, subjectInfoBo1);
//            return subjectInfoBo1;
//        }).collect(Collectors.toList());
//        pageResult.setRecords(subjectInfoBoList);
//        pageResult.setTotal(count);
//        pageResult.setStart(start);
//        return pageResult;
//    }
}
